package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.AddressBean;
import com.wude.bean.UserInfo;
import com.wude.guild.AddAddressActivity;
import com.wude.guild.R;
import com.wude.guild.adapter.AddresAdapter;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/23 0023.
 * <p>
 * 收货地址
 */

public class ShouHuoFragment extends TitleBaseFragment {
    private Unbinder unbinder;

    @BindView(R.id.dizhi_list)
    ListView dizhiList;
    @BindView(R.id.error_text)
    TextView errorText;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    private AddresAdapter addresAdapter;
    private UserInfo loginUser;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_address, container, false);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("ShouHuoFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);

        setHeaderTitle("地址列表");
        TextView rightTextView = getTitleHeaderBar().getRightTextView();
        rightTextView.setText("添加地址");
        rightTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), AddAddressActivity.class));
            }
        });

        addresAdapter = new AddresAdapter(getActivity(), this);
        dizhiList.setAdapter(addresAdapter);
    }

    public void initdata() {
        loginUser = Utils.getLoginUser();
        HashMap<String, String> map = new HashMap<>();
        map.put("token", loginUser.token);
        HttpCom.POST(handler, HttpCom.getHuoQuAddressURL(), map, false);
    }


    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    List<AddressBean> addressBeen = HttpUtils.DNSAddress(msg.obj.toString());
                    if (addressBeen != null) {
                        errorLayout.setVisibility(View.GONE);
                        dizhiList.setVisibility(View.VISIBLE);
                        addresAdapter.setList(addressBeen);
                    } else {
                        errorLayout.setVisibility(View.VISIBLE);
                        dizhiList.setVisibility(View.GONE);
                        errorText.setText("尚未添加收货地址");
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        initdata();
        MobclickAgent.onPageStart("ShouHuoFragment");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
